import {
  getSblxBySbzlx
} from '../../../../../mapUtils/SblxUtil';
import IpmsPoint from '../IpmsPoint';
import RuleResult from '../vo/RuleResult';

/**
 * 柱上断路器修改
 * @implements {IpmsPoint}
 */
class PmsZsdlq extends IpmsPoint {
  constructor(_tool) {
    super(_tool);
    this.defaultSbzlx = 11100000;
    this.defaultDydj = 22;
    this.drawtype = 'pms_zsdlq';
    this.defaultName = '新增柱上断路器';
    this.defaultIcon = 'zskg_default_0';
    this.defaultCut = true;
  }

  /**
   * 新增柱上断路器
   * @override
   * @param {Object} option 默认参数
   * @param {Array} snapList 捕捉图形
   */
  draw(option, snapList) {
    return super.draw(option, snapList);
  }

  /**
   * 编辑柱上断路器
   * @override
   * @param {Object} option 默认参数
   * @param {Array} snapList 捕捉图形
   */
  edit(option, snapList) {
    return super.edit(option, snapList);
  }

  /**
   * 获取设备坐标
   * @override
   */
  // getGeometry() {
  //   return turf.lineString([this.geometry.coordinates, this.geometry.coordinates]).geometry;
  // }

  /**
   * 获取当前绘制设备信息
   * @override
   */
  async getTargetDevice() {
    // 判断柱上变压器连接得是否为虚拟点，返回不同得数据
    var startDevice = this.getStartDevice();
    var data = null;
    if (this.defaultCut) {
      var sliceInfo = this.getSliceInfo();
      var {
        start,
        finish,
        between,
        rotate
      } = sliceInfo;
      data = {
        schema: this.option.schema,
        versionid: this.option.versionid || '',
        ghrw: this.option.ghrw || '',
        ghfa: this.option.ghfa || '',
        ghnf: this.option.ghnf || '',
        ghxm: this.option.ghxm || '',
        fxlx: this.option.fxlx || 2,
        dydj: this.option.dydj || this.defaultDydj,
        away: 0,
        sblx: getSblxBySbzlx(this.defaultSbzlx),
        sbzlx: this.defaultSbzlx,
        otherfields: {
          sbmc: this.option.sbmc || this.defaultName,
          shape: this.getGeometryWkt(this.geometry),
          dydj: this.option.dydj || this.defaultDydj,
          ssqy: this.option.ssqy || ''
        },
        startDevice: startDevice,
        sliceInfo: startDevice ? {
          start: this.getGeometryWkt(start.geometry),
          finish: this.getGeometryWkt(finish.geometry),
          between: this.getGeometryWkt(between.geometry),
          rotate: rotate,
          intersects: sliceInfo.intersects
        } : {
          start: '',
          finish: '',
          between: '',
          rotate: 0,
          intersects: []
        },
        defaultCut: 1,
      };
    }else {
      data = {
        schema: this.option.schema,
        versionid: this.option.versionid || '',
        ghrw: this.option.ghrw || '',
        ghfa: this.option.ghfa || '',
        ghnf: this.option.ghnf || '',
        ghxm: this.option.ghxm || '',
        fxlx: this.option.fxlx || 2,
        dydj: this.option.dydj || this.defaultDydj,
        away: 0,
        sblx: getSblxBySbzlx(this.defaultSbzlx),
        sbzlx: this.defaultSbzlx,
        otherfields: {
          sbmc: this.option.sbmc || this.defaultName,
          shape: this.getGeometryWkt(startDevice.geometry),
          dydj: this.option.dydj || this.defaultDydj,
          ssqy: this.option.ssqy || ''
        },
        defaultCut: 0,
      }
    }
    return data;
  }

  /**
   * 验证点合理性
   * @override
   * @param {Number} index 点下标
   * @param {*} snap 捕捉到的设备
   */
  async _ruleJudgePoint(index, snap) {
    let layer = snap.layer;
    if (layer) {
      // 如果不是空点
      if (this._isLine(layer.properties.sbzlx)) {
        var sliceInfo = this.getSliceInfo();
        var {
          start,
          finish,
          between
        } = sliceInfo;
        if (start && finish && between) {
          return new RuleResult(true, index, 'success');
        }
        return new RuleResult(false, index, '生成柱上断路器失败');

      }
      // 柱上断路器连接虚拟点
      // if (layer.properties.sbzlx == 10200002) {
      //   this.defaultCut = false;
      //   return new RuleResult(true, index, 'success');
      // }
      // 其它情况不允许通过
      return new RuleResult(false, index, '柱上断路器需要和架空线路连接');

    }
    // 如果是空点
    return new RuleResult(false, index, '柱上断路器需要和架空线路连接');

  }

  /**
   * 是否是允许捕捉的设备
   * @param {Number} sbzlx 设备子类型
   */
  _isPermittedDevice(sbzlx) {
    // 不能捕捉电力电缆
    let notPermittedDeviceSbzlx = [20100000];
    return notPermittedDeviceSbzlx.indexOf(sbzlx) < 0;
  }
}

export default PmsZsdlq;